package com.chz.SupermarketMS.controllers;

import com.chz.SupermarketMS.dao.CommodityRepository;
import com.chz.SupermarketMS.dao.KindRepository;
import com.chz.SupermarketMS.dto.KindCommodities;
import com.chz.SupermarketMS.entities.*;
import com.chz.SupermarketMS.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("commodities")
public class CommodityController {

    @Autowired
    CommodityService commodityService;

    @Autowired
    CommodityRepository commodityRepository;

    @Autowired
    KindRepository kindRepository;


    @GetMapping
    public String displayCommodity(Model model){
        List<Commodity> commodities = commodityService.getAll();
        model.addAttribute("commodities",commodities);

        List<KindCommodities> kindCommodities = commodityRepository.kindCommodities();
        model.addAttribute("kindCommodities",kindCommodities);

        return "commodities/list-commodities";
    }

    //响应访问新建项商品页面的GET请求
    @GetMapping("/new")
    public String displayCommodityForm(Model model){

        Commodity aCommodity = new Commodity();
        model.addAttribute("commodity", aCommodity);

//        List<Kind> kinds = kindRepository.findAll();
//        model.addAttribute("allKinds",kinds);

        return "commodities/new-commodity";
    }


    //表单提交时发送给服务器端的POST请求
    @PostMapping("/save")
    public String createCommodity(Model model, @Valid Commodity commodity, Errors errors){

        if (errors.hasErrors()) return "commodities/new-commodity";

        //处理保存到数据库的行为
        commodityService.save(commodity);

//        Iterable<Kind> chosenKinds = kindRepository.findAllById(kinds);
//        kindRepository.saveAll(chosenKinds);

        //防止重复提交
        return "redirect:/commodities";
    }
    @GetMapping("/update")
    public String displayCommodityUpdateForm(@RequestParam("id") long theId, Model model) {
        Commodity theCommodity = commodityService.findByCommodityId(theId);
        model.addAttribute("commodity", theCommodity);
        return "commodities/new-commodity";
    }
    @GetMapping("/delete")
    public String deleteCommodity(@RequestParam("id")long theId, Model model) {
        Commodity theCommodity = commodityService.findByCommodityId(theId);
        commodityService.delete(theCommodity);
        return "redirect:/commodities";
    }



}
